#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/7/11 8:02 下午
# @Author  : WangZhixing


# 1、将cluster中节点的label以及别的特征提取出来，并输入对应dataset中的label中。
# 2、把我们的mdg文件的node节点，改成数字。
# 3、把我们的node文件中的feature放到我们的dataset中的feature中。
# 4、获取节点的邻接权重矩阵
import pandas as pd
import json

import torch


class Process():
    # 初始化我们的dataset数据集

    def __init__(self):
        self.idx = 0  # 节点idx
        self.dic = {}  # [节点idx：节点name]
        self.lst=[]# [[节点1,节点2,节点weight]]
        self.NodeLabel = {}  # [节点idx：节点label]

    def Label2File(self, cluster,label_path):
        with open(cluster, 'r') as f:
            label_idx = 1
            label = {}
            data = f.read().split('\n')[:-1]
            # 提取label转化为数字
            for line in data:
                x = line.split(' ')
                cluster = x[1]
                node = x[2]
                if cluster not in label:
                    label[cluster] = label_idx
                    label_idx += 1
                if node in self.dic:
                    self.NodeLabel[self.dic[node]] = label[cluster]

            labelpd = pd.DataFrame()
            idx = list(self.NodeLabel.keys())
            label = list(self.NodeLabel.values())
            labelpd["idx"] = idx
            labelpd["label"] = label
            labelpd.to_csv(label_path,sep=" ", header=0, index=0)


    def Node2Num(self, raw_path, edge_path,name_path):
        with open(raw_path, 'r') as f:
            data = f.read().split('\n')[:-1]
            for line in data:
                x = line.split()
                s = x[1]
                t = x[2]

                if s not in self.dic:
                    self.dic[s] = self.idx
                    self.NodeLabel[self.idx] = 0
                    self.idx += 1

                if t not in self.dic:
                    self.dic[t] = self.idx
                    self.NodeLabel[self.idx] = 0
                    self.idx += 1
                self.lst.append([self.dic[s], self.dic[t], 1])

            name = ['src', 'dst', 'value']
            test = pd.DataFrame(columns=name, data=self.lst)
            test.to_csv(edge_path,sep=" ",header=0,index=0)

            # name
            namepd = pd.DataFrame()
            name = list(self.dic.keys())
            idx = list(self.dic.values())
            namepd["idx"]=idx
            namepd["name"]=name
            namepd.to_csv(name_path,sep=" ",header=0,index=0)

